package com.kyrie.lib_sys.application.publisherApplys.controller;

import com.google.common.base.Preconditions;
import com.kyrie.lib_sys.common.entity.PageResult;
import com.kyrie.lib_sys.common.entity.Result;
import com.kyrie.lib_sys.common.pojo.dto.PublisherDTO;
import com.kyrie.lib_sys.common.pojo.dto.PublisherPageQueryDTO;
import com.kyrie.lib_sys.application.publisherApplys.service.PublisherService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 出版社相关controller类
 *
 * @author heefM
 * @date 2025-08-07
 */
@RestController
@RequestMapping("/publisher/")
@Slf4j
public class PublisherController {

    @Autowired
    private PublisherService publisherService;

    @RequestMapping("/add")
    public Result addPublisher(@RequestBody PublisherDTO publisherDTO) {
        if (log.isInfoEnabled()) {
            log.info("添加出版社Controller:addPublisher: {}", publisherDTO);
        }
        Preconditions.checkArgument(!StringUtils.isBlank(publisherDTO.getName()), "出版社名称不能为空");

        publisherService.insert(publisherDTO);
        return Result.success("添加出版社成功");
    }

    /**
     * 根据id查询出版社
     *
     * @param id
     * @return
     */
    @RequestMapping("/queryById")
    public Result<PublisherDTO> queryPublisherById(@RequestParam Long id) {
        log.info("根据id查询出版社: {}", id);
        Preconditions.checkNotNull(id, "出版社id不能为空");
        PublisherDTO publisherDTO = publisherService.queryPublisherById(id);

        return Result.success(publisherDTO);
    }

    /**
     * 分页查询所有出版社
     *
     * @return
     */
    @RequestMapping("/list")
    public Result<PageResult> queryPage(PublisherPageQueryDTO publisherPageQueryDTO) {

        log.info("分页查询所有出版社: {}", publisherPageQueryDTO);
        // 设置默认分页参数
        if (publisherPageQueryDTO.getPageNum() == null) {
            publisherPageQueryDTO.setPageNum(1);
        }
        if (publisherPageQueryDTO.getPageSize() == null) {
            publisherPageQueryDTO.setPageSize(10);
        }
        PageResult pageResult = publisherService.queryPage(publisherPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 条件查询出版社
     *
     * @param publisherDTO
     * @return
     */
    @RequestMapping("/queryByCondition")
    public Result<PublisherDTO> queryByCondition(@RequestBody PublisherDTO publisherDTO) {
        if (log.isInfoEnabled()) {
            log.info("条件查询出版社: {}", publisherDTO);
        }
        publisherDTO = publisherService.queryByCondition(publisherDTO);
        return Result.success(publisherDTO);
    }

    /**
     * 修改出版社
     *
     * @param publisherDTO
     * @return
     */
    @RequestMapping("/update")
    public Result updatePublisher(@RequestBody PublisherDTO publisherDTO) {
        if (log.isInfoEnabled()) {
            log.info("修改出版社Controller:updatePublisher: {}", publisherDTO);
        }
        Preconditions.checkArgument(publisherDTO != null, "出版社信息不能为空");
        publisherService.update(publisherDTO);
        return Result.success("修改出版社成功");
    }

    /**
     * 根据id删除出版社
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result<String> deletePublisher(@RequestParam("id") Long id) {
        if (log.isInfoEnabled()) {
            log.info("删除出版社Controller:deletePublisher: {}", id);
        }
        publisherService.deleteById(id);
        return Result.success("删除出版社成功");
    }

    /**
     * 批量删除出版社
     *
     * @param ids
     * @return
     */
    @RequestMapping("/deleteBatch")
    public Result<String> deleteBatch(@RequestParam List<Long> ids) {
        if (log.isInfoEnabled()) {
            log.info("批量删除出版社Controller:deleteBatch: {}", ids);
        }
        publisherService.deleteBatch(ids);
        return Result.success("批量删除出版社成功");
    }

    /**
     * 批量新增出版社
     *
     * @param publisherDTOS
     * @return
     */
    @RequestMapping("/insertBatch")
    public Result insertBatch(@RequestBody List<PublisherDTO> publisherDTOS) {
        if (log.isInfoEnabled()) {
            log.info("批量新增出版社insertBatch: {}", publisherDTOS);
        }
        Preconditions.checkArgument(publisherDTOS != null && publisherDTOS.size() > 0, "出版社列表不能为空");
        publisherService.insertBatch(publisherDTOS);
        return Result.success("批量新增出版社成功");
    }
}
